const db = wx.cloud.database()
Page({
  data: {
    fileinfo: { istop: 0, fileImg:[]}
  },
  updateValue: function (event) {
    let name = event.currentTarget.dataset.name;
    let fileinfo = this.data.fileinfo
    fileinfo[name] = event.detail.value
    this.setData({
      fileinfo: fileinfo
    })
  },
  switchChange: function (e) {
    let fileinfo = this.data.fileinfo
    if (e.detail.value == true) {
      fileinfo.istop=1
    }
    else {
      fileinfo.istop = 0
    }
    this.setData({
      fileinfo: fileinfo
    })
  },
  chooseFile() {
    var that = this
    let fileinfo = this.data.fileinfo
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {

        wx.showLoading({
          title: '文件上传中...',
        })
        const filename = res.tempFiles[0].name
        var tempPath = res.tempFiles[0].path
        console.log(filename)
        console.log(tempPath)
        var randString = Math.floor(Math.random() * 1000000).toString()
        var filepath = randString + filename
        wx.cloud.uploadFile({
          cloudPath: 'file/' + filepath,
          filePath: res.tempFiles[0].path,
        }).then(res => {
          var filename1 = filename;
          var index1 = filename1.lastIndexOf(".");
          var index2 = filename1.length;
          var type = filename1.substring(index1, index2);
          var FileImg=[]
          if (type == '.pdf') {
            FileImg[0] = '/images/pdf.png'
          }
          else if (type == '.doc' || type == '.docx') {
            FileImg[0] = '/images/word.png'
          }
          else if (type == '.xls' || type == '.xlsx') {
            FileImg[0] = '/images/excel.png'
          }
          else
          {
            FileImg[0] = '/images/file.png'
          }
          fileinfo.fileID = res.fileID
          fileinfo.filename =filename
          fileinfo.fileImg = FileImg
          that.setData({ fileinfo: fileinfo })
          wx.hideLoading()
        }).catch(error => {
          // handle error
        })
      }
    })
  },
  deleteFile: function (event) {
    let fileinfo = this.data.fileinfo
    var fileID = fileinfo.fileID
    console.log(fileID)
    wx.cloud.deleteFile({
      fileList: [fileID]
    }).then(res => {
      fileinfo.fileImg=[]
      fileinfo.filename=''
      this.setData({ fileinfo, fileinfo})
    }).catch(error => {
      // handle error
    })
  },

  submitform: function (event) {
    let fileinfo = this.data.fileinfo
    db.collection('files').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        title: fileinfo.title,
        content: fileinfo.content,
        publisher: fileinfo.publisher,
        fileID: fileinfo.fileID,
        fileImg: fileinfo.fileImg,
        filename: fileinfo.filename,
        istop: fileinfo.istop,
        submitdate: db.serverDate()
      }
    })
      .then(res => {
        wx.showToast({
          title: '文件添加成功',
          icon: 'success',
          duration: 2000
        })
        wx.navigateTo({
          url: '../filelist/filelist'
        })
      })
      .catch(console.error)
  },
})